package product_type

import (
	"context"
	"gitee.com/zyw0605688_admin/gozero/product_service/model"

	"gitee.com/zyw0605688_admin/gozero/product_service/internal/svc"
	"gitee.com/zyw0605688_admin/gozero/product_service/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductTypeUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductTypeUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductTypeUpdateLogic {
	return &ProductTypeUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductTypeUpdateLogic) ProductTypeUpdate(req *types.ProductTypeUpdateRequest) (resp *types.ProductTypeUpdateResponse, err error) {
	// 先根据id查出数据库里的结果
	item := model.ProductType{}
	err = l.svcCtx.DB.Where("id = ?", req.Id).First(&item).Error
	if err != nil {
		return
	}
	// 根据参数修改件结果
	item.ProductTypeName = req.ProductTypeName
	item.ProductTypeCode = req.ProductTypeCode
	item.ProductTypeDesc = req.ProductTypeDesc
	item.SortCode = req.SortCode
	// 保存回数据库
	err = l.svcCtx.DB.Save(&item).Error
	if err != nil {
		return
	}
	resp = &types.ProductTypeUpdateResponse{
		Id:              item.Id,
		ProductTypeName: item.ProductTypeName,
		ProductTypeCode: item.ProductTypeCode,
		ProductTypeDesc: item.ProductTypeDesc,
		SortCode:        item.SortCode,
		CreatedAt:       item.CreatedAt,
	}
	return
}
